// 2013-9-15下午03:22:23  net.betou.trade.main.manager.impl.ConfigMngImpl.java

package net.betou.trade.main.manager.impl;

import net.betou.common.hibernate3.Updater;
import net.betou.common.hibernate3.Updater.UpdateMode;
import net.betou.common.page.Pagination;
import net.betou.common.util.DataConvertUtils;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Config;
import net.betou.trade.main.dao.ConfigDao;
import net.betou.trade.main.manager.ConfigMng;
import net.betou.trade.main.manager.agencyorproxy.AdminMng;
import net.betou.web.Constants;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class ConfigMngImpl implements ConfigMng{

	@Transactional( readOnly = true )
	public Config findById( Integer id ) {
		return dao.findById( id );
	}

	@Transactional( readOnly = true )
	public Pagination getPage( String searchProperty, String searchValue, String orderProperty, String orderDirection, String filterStatus, int pageNo, int pageSize ) {
		return dao.getPage( searchProperty, searchValue, orderProperty, orderDirection, filterStatus, pageNo, pageSize );
	}

	//@Transactional( readOnly = true )
	public Config findConfig( AgentAndAdmins admin ) {

		boolean system = admin.getSystem( );

		Config config;
		// 这里要避免如果不是系统管理员先进来的时候，默认为空
		if( system ){
			config = initDefault( dao.findByDefault( ) );
		}else{
			Integer adminId = getAdminId( admin );
			if( adminId > 0 ){
				config = dao.findByAdmin( adminId );
			}else{
				config = null;
			}
		}
		// 如果没有的话，就要使用系统默认的
		if( config == null ){
			config = initDefault( dao.findByDefault( ) );
		}

		return config;
	}

	public Config [ ] deleteByIds( Integer [ ] ids ) {
		Config [ ] beans = new Config [ ids.length ];

		for ( int i = 0; i < ids.length; i++ ){
			beans[ i ] = dao.deleteById( ids[ i ] );
		}
		return beans;
	}

	public Config save( Config bean, AgentAndAdmins admin, String ip ) {

		bean.init( );

		bean.setCreateIp( ip );
		bean.setAdmin( admin );
		bean.setConfigDefault( false );

		bean = dao.save( bean );

		return bean;
	}

	public Config initDefault( ) {
		Config bean = new Config( );
		bean.init( );

		bean.setCreateIp( Constants.DEFAULT_IP );
		bean.setLogo( Constants.DEFAULT_LOGO );
		bean.setName( Constants.DEFAULT_SITE_NAME );
		bean.setAdmin( null );
		bean.setConfigDefault( true );

		bean = dao.save( bean );

		return bean;
	}

	public Config update( Config bean, String name, String logo ) {
		
		bean.setName( name );
		bean.setLogo( logo );
		
		Updater< Config > updater = new Updater< Config >( bean, UpdateMode.MIN );

		updater.include( "name" );
		updater.include( "logo" );

		bean = dao.updateByUpdater( updater );

		return bean;
	}

	private Integer getAdminId( AgentAndAdmins admin ) {

		// 只要是机构或者代理商 就可以有自己的配置
		if( isAgencyOrProxy( admin ) ){
			return admin.getId( );
		}

		String topIntermedia = admin.getTopIntermedia( );

		if( "0".equals( topIntermedia ) ){
			return admin.getId( );
		}

		String [ ] tops = StringUtils.split( topIntermedia, "," );

		if( tops == null || tops.length == 0 ){
			return 0; // 就都使用默认的
		}

		for ( int i = tops.length - 1; i >= 0; i-- ){
			AgentAndAdmins bean = adminMng.findById( DataConvertUtils.getInt( tops[ i ] ) );
			if( isAgencyOrProxy( bean ) ){
				return bean.getId( );
			}
		}

		return 0;
	}

	/**
	 * 是机构的话就返回true
	 * @version 1.1
	 * @param admin
	 * @return
	 */
	private boolean isAgencyOrProxy( AgentAndAdmins admin ) {
		if( !admin.getJustAdmin( ) && !admin.getSystem( ) && admin.getAgencyOrProxy( ) ){
			return true;
		}
		return false;
	}

	/**
	 * 如果config 为 null, 则使用 default
	 * 
	 * @param config
	 * @return
	 */
	private Config initDefault( Config config ) {
		if( config != null ){
			return config;
		}

		return initDefault( );
	}

	@Autowired
	private ConfigDao dao;
	
	@Autowired
	private AdminMng adminMng;

}
